//
//  ModifySecondaryPasswordController.swift
//  SCA
//
//  Created by tjun on 2025/9/6.
//

import UIKit


class ModifySecondaryPasswordController: BaseController {
    
    private var scrollView: UIScrollView!
    private var contentView: UIView!
 
    private let passwordTextField = LabeledTextField(tip: "二级安全密码 的最少字符为6个。".localizedString)
    private let confirmPasswordTextField = LabeledTextField(tip: "二级安全密码 的最少字符为6个。".localizedString)
    private let otpTextField = LabeledTextField()
    private let confirmButton = MainButton(type: .system, title: "确认".localizedString, font: UIFontMake(16), textColor: .white)

    var lastView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.title = "更改二级安全密码".localizedString
        setupUI()
    }
    
    private func setupUI() {
        
        scrollView = UIScrollView(frame: CGRect(x: 0, y: navBarHeight, width: view.width, height: screenHeight - navBarHeight)).addTo(view)
        scrollView.keyboardDismissMode = .onDrag
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = true
        scrollView.contentInsetAdjustmentBehavior = .never
        
        let contentSize = CGSize(width: scrollView.width, height: scrollView.height)
        
        scrollView.contentSize = contentSize
        
        contentView = UIView().addTo(scrollView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }
 
        passwordTextField.titleAttributedText = "二级安全密码*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        contentView.addSubview(passwordTextField)
        passwordTextField.textField.keyboardType = .asciiCapable
        passwordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        passwordTextField.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
        }

        confirmPasswordTextField.titleAttributedText = "确认二级安全密码**".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        confirmPasswordTextField.textField.keyboardType = .asciiCapable
        confirmPasswordTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        contentView.addSubview(confirmPasswordTextField)
        confirmPasswordTextField.snp.makeConstraints { make in
            make.top.equalTo(passwordTextField.snp.bottom).offset(10)
            make.left.right.equalTo(passwordTextField)
        }

        otpTextField.titleAttributedText = "2FA OTP*".attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        contentView.addSubview(otpTextField)
        otpTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        otpTextField.snp.makeConstraints { make in
            make.top.equalTo(confirmPasswordTextField.snp.bottom).offset(10)
            make.left.right.equalTo(passwordTextField)
        }
        
        confirmButton.addTo(contentView)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalTo(passwordTextField)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.top.equalTo(otpTextField.snp.bottom).offset(40)
        }
        
        lastView = confirmButton
    }

    @objc func textFieldDidChange(_ sender: UITextField) {
        if let password = passwordTextField.textField.text,
           let confirmPassword = confirmPasswordTextField.textField.text,
           let otp = otpTextField.textField.text,
           password.count >= 6,
           confirmPassword.count >= 6,
           password == confirmPassword,
           otp.count > 0 {
            confirmButton.isEnabled = true
        } else {
            confirmButton.isEnabled = false
        }
    }
 
    @objc func confirmButtonTapped() {
        
        guard let password = passwordTextField.textField.text,
           let _ = confirmPasswordTextField.textField.text,
              let otp = otpTextField.textField.text else {
            return
        }
        
        view.endEditing(true)
        
        HUD.showLoadingInView(self.view)
        MineService.setLevel2Password(password: password, otf: otp) {
            HUD.hideHUD(for: self.view)
            
            let successController = SetLevel2PasswordSuccessController(text: "更改二级安全密码成功".localizedString)
            self.navigationController?.pushViewController(successController, animated: true)
            self.navigationController?.viewControllers = self.removeNavChildViewControllers(["ModifySecondaryPasswordController"])
        }
    }
 
}

 

